/**
 * Copyright (c) 2021 zhaodongyang
   CurtainJs is licensed under Mulan PSL v2.
   You can use this software according to the terms and conditions of the Mulan PSL v2.
   You may obtain a copy of Mulan PSL v2 at:
            http://license.coscl.org.cn/MulanPSL2
   THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
   See the Mulan PSL v2 for more details.
 */

import prompt from '@system.prompt';
import {config} from '../../common/js/config.js'

export default {
    data: {
        todolist: ["this", "is", "a", "better", "guide", "view", "generate", "lib",
        ".", "for", "android", "more", "and", "more", "useful", "to", "simplify",
        "your", "coding", ".", "have", "a", "nice", "day", "~"],
        canvasColor: "#333333",
        configList: [new config(0, 0, 0, 0, ""), new config(0, 0, 0, 0, "")],
        isInit: true
    },
    onInit() {
    },
    onItemClick: function (position, data) {
        prompt.showToast({
            message: '点击了第：' + position + '个， 标题： ' + data
        })
    },
    initHighLightData(rootView) {
        const context = this
        try {
            context.data.configList.splice(0)
            let listView = rootView.$element('listView')
            let rootTop = listView.getBoundingClientRect().top

            let item2 = rootView.$element('item-2')
            let item2Top = item2.getBoundingClientRect().top

            let item5 = rootView.$element('item-5')
            let item5Top = item5.getBoundingClientRect().top
            //说明第一个 第三个都显示了
            if ((item2Top - rootTop >= 0) && (item5Top - rootTop >= 0)) {
                context.addItemToList(context.data.configList, item2, 'item-2')
                context.addItemToList(context.data.configList, item5, 'item-5')
            }
            context.data.isInit = false
        } catch (err) {
            console.log("测试打印===initHighLightData err===" + err)
        }
    },
    dispatchTouch(e) {
        this.isInit = false
    },
    dispatchScroll(e) {
        //stateValue: 0表示列表滑动已经停止。
        if (e == null || e.scrollX == null || e.scrollY == null || e.scrollState == null) {
            return
        }
        let scrollX = e.scrollX
        let scrollY = e.scrollY
        let scrollState = e.scrollState

        if (this.isInit) {
            return
        }

        if (scrollState == 0) {
            const context = this
            try {
                context.configList.splice(0)
                let listView = context.$element('listView')
                let rootTop = listView.getBoundingClientRect().top

                let item1 = context.$element('img-1')
                let item1MarginLeft = 10 // 无法动态获取css里面的MarginTop 本地写死
                let item1Top = item1.getBoundingClientRect().top

                let item3 = context.$element('text-3')
                let item3MarginLeft = 10 // 无法动态获取css里面的MarginTop 本地写死
                let item3Top = item3.getBoundingClientRect().top
                //说明第一个 第三个都显示了
                if ((item1Top - rootTop >= 0) && (item3Top - rootTop > 0)) {

                    context.addItemToList(context.configList, item1, 'img-1', "circle", item1MarginLeft, 0)
                    context.addItemToList(context.configList, item3, 'text-3', "rect", item3MarginLeft, 0)
                    context.$emit('eventType1', {
                        type: "showItem",
                        canShow: true,
                        canvasColor: context.canvasColor,
                        list: context.configList
                    });
                }
            } catch (err) {
                console.log("测试打印===dispatchScroll err===" + err)
            }
        }
    },
    addItemToList(list, item, id, highLightType='rect', marginLeft=0, marginTop=0) {
        let tempLeft = item.getBoundingClientRect().left
        let tempTop = item.getBoundingClientRect().top
        let tempWidth = item.getBoundingClientRect().width
        let tempHeight = item.getBoundingClientRect().height

        let bean = {
            left: tempLeft + marginLeft,
            top: tempTop + marginTop,
            width: tempWidth - marginLeft,
            height: tempHeight - marginTop,
            id: id,
            isDraw: true,
            type: highLightType
        }
        list.push(bean)
    },
}